package com.zhdl.modules.eastdosing.service.impl;

import com.google.common.io.Files;
import com.project.common.util.DelayUtil;
import com.zhdl.hardware_modules.service.impl.DeviceServiceImpl;
import com.zhdl.modules.autopick.entity.CameraCoordinate;
import com.zhdl.modules.autopick.repository.CameraRepository;
import com.zhdl.modules.eastdosing.service.CameraEastService;
import com.zhdl.modules.tools.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.StandardCopyOption;
import java.util.Optional;

import static com.zhdl.modules.eastdosing.EastDosingProcess.isRunning;

@Slf4j
@Service
public class CameraEastServiceImpl implements CameraEastService {

    @Resource
    private CameraRepository cameraRepository;

    /**
     * 相机拍照
     */
    @Override
    public void takePhoto() {

        try {
            String code = HttpClientUtil.get("http://localhost:8081/api/v1" + "/one/shot/");
            log.info("code: " + code);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        DelayUtil.delayedExecution(200, () -> {
            File file = new File("E:\\BaiduSyncdisk\\project\\photo");//输入要删除文件目录的绝对路径
            File[] files = file.listFiles();
            for (File value : files) {
                // 删除子文件
                if (value.isFile()) {
                    boolean delete = value.delete();
                    log.info("当前文件已经删除---" + value.getName() + "删除结果为" + delete);
                }
            }
            /* 将结果删掉为空 */
            String pathResult = "E:\\BaiduSyncdisk\\project\\result\\subworldauto.txt";
            File fileResult = new File(pathResult);
            boolean delete = fileResult.delete();
            log.info("删除文件夹成功结果" + delete);

            DelayUtil.delayedExecution(4000, () -> {
                try {
                    String code = HttpClientUtil.get("http://localhost:8081/api/v1" + "/one/shot/");
                    log.info("code: " + code);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        });

        log.info("通知相机拍照");
    }

    /**
     * 获取拍照结果
     */
    @Override
    public void getComputeResult() {
//        try {
//            /* 查出数据库结果 */
//            Optional<CameraCoordinate> byId = cameraRepository.findById(1);
//            CameraCoordinate coordinate = byId.get();
//            /* 调用Matlab方法 */
//            DeviceServiceImpl.getELocation.AcquireTargetLocation3E(1,  coordinate.getFlorNum() * 1.0f);
//            File file = new File("E:\\BaiduSyncdisk\\project\\result\\subworldauto.txt");
//            /* 读取文件结果 */
//            String string = Files.toString(file, Charset.defaultCharset());
//
//            /*如果结果为空,则直接return*/
//            if (string.isEmpty()) {
//                isRunning = false;
//                return;
//            }
//            /* -2788.69,-578.39,270.00,1.00|-2350.75,-581.89,270.00,1.00|-1955.69,-581.01,270.00,1.00|-2649.12,-75.70,270.00,0.00|-2023.94,-77.45,270.00,0.00| */
//            if (coordinate.getFlorNum() != 8) {
//                coordinate.setFlorNum(coordinate.getFlorNum() + 1);
//            } else {
//                coordinate.setFlorNum(1);
//            }
//            /* 只要层数有变化，则将袋数从头开始 */
//            coordinate.setBagNum(0);
//            coordinate.setCameraParam(string);
//            cameraRepository.save(coordinate);
//            log.info("当前层数为{},袋数为{},结果为{}", coordinate.getFlorNum(), coordinate.getBagNum(), coordinate.getCameraParam());
//        } catch (Exception e) {
//            /* 如果抛出异常，则剪切照片，重试 */
//            String path = "E:\\BaiduSyncdisk\\project\\photo\\";
//            File file = new File(path);
//            File[] files = file.listFiles();
//            String currentFileName = files[0].getName();
//
//            File file1 = new File(path + currentFileName);
//            File file2 = new File("E:\\BaiduSyncdisk\\project\\backupPhoto\\" + currentFileName);
//            try {
//                java.nio.file.Files.move(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);
//            } catch (IOException e1) {
//                log.info(e1.getMessage());
//            }
//            log.info("当前照片识别失败");
//            isRunning = false;
//        }
//        log.info("拍照拿到照片 计算机器人需要移动坐标");
    }

}
